package com.link.base.base.visitplan.service;

import com.link.base.base.visitplan.dao.mybatis.mapper.VisitRecordMapper;
import com.link.base.base.visitplan.model.VisitPlan;
import com.link.base.base.visitplan.model.VisitRecord;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author unknown
 * @date unknown
 * @comments 拜访记录
 */
@Service
@Transactional(rollbackFor = ServiceException.class)
public class VisitRecordServiceImpl extends BasicServiceImpl<VisitRecord> implements VisitRecordService {

    @Resource
    private VisitRecordMapper visitRecordMapper;

    @Resource
    private VisitPlanService visitPlanService;

    @Override
    public BasicMapper<VisitRecord> getBasicMapper() {
        return visitRecordMapper;
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void batchInsert(List<VisitRecord> list) throws Exception {
        VisitPlan plan = null;
        if (list != null && list.size() > 0) {
            VisitPlan example = new VisitPlan();
            example.setId(list.get(0).getPlanNo());
            plan = visitPlanService.queryById(example);
        }
        if (null != plan) {
            for (int i = 0; list != null && i < list.size(); i++) {
                VisitRecord model = list.get(i);
                model.setId(keyGenerateService.keyGenerate());
                model.setVisitTime(plan.getBeginTime());
                model.setEndDate(plan.getEndTime());
                model.setPlanDesc(plan.getDec());
                getBasicMapper().insert(list.get(i));
            }
        }
    }
}
